package com.bluetoothsms.common;

import java.text.DateFormat;
import java.util.Date;

import com.bluetoothsms.common.Common.SmsStatus;
import com.bluetoothsms.common.Common.SmsType;

public class Sms {

	public final static int FIELD_COUNT = 6;

	private final String mText;
	private final Date mDate;
	private final SmsType mType;
	private final int mThreadId;
	private final String mNumber;
	private final SmsStatus mStatus;

	public Sms(String text, long date, SmsType type, int threadId,
			String number, SmsStatus status) {
		mText = text;
		mDate = new Date(date);
		mType = type;
		mThreadId = threadId;
		mNumber = number;
		mStatus = status;
	}

	public Sms(String text, long date, int type, int threadId, String number,
			int status) {
		this(text, date, SmsType.fromAndroidType(type), threadId, number,
				SmsStatus.fromAndroidStatus(status));
	}

	public Sms(String[] data, int firstIdx) {
		if (data.length - firstIdx < FIELD_COUNT)
			throw new IllegalArgumentException("Too few data fields!");

		mText = data[firstIdx++];
		mDate = new Date(Long.valueOf(data[firstIdx++]));
		mType = SmsType.values()[Integer.valueOf(data[firstIdx++])];
		mThreadId = Integer.valueOf(data[firstIdx++]);
		mNumber = data[firstIdx++];
		mStatus = SmsStatus.values()[Integer.valueOf(data[firstIdx++])];
	}

	public String getText() {
		return mText;
	}

	public Date getDate() {
		return mDate;
	}

	public SmsType getType() {
		return mType;
	}

	public int getThreadId() {
		return mThreadId;
	}

	public String getNumber() {
		return mNumber;
	}

	public SmsStatus getStatus() {
		return mStatus;
	}

	public String getDateString() {
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
				DateFormat.MEDIUM);

		StringBuilder b = new StringBuilder();
		b.append(mType.toString());
		b.append(": ");
		b.append(df.format(mDate));

		if (!mStatus.equals(SmsStatus.Received)) {
			b.append(" - Status: ");
			b.append(mStatus.toString());
		}

		return b.toString();
	}

	@Override
	public String toString() {
		return getDateString() + '\n' + mText;
	}

}
